﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _1061102_1061114_1061129_1061132_1061141.Models;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using PagedList;
using BanDTBLL;

namespace _1061102_1061114_1061129_1061132_1061141.Controllers
{
    public class QuanLiSanPhamController : Controller
    {
        //
        // GET: /QuanLiSanPham/
        ban_dien_thoai_dbEntities store = new ban_dien_thoai_dbEntities();
        public ActionResult Index()
        {

            return RedirectToAction("ListSanPham");
        }

        public ActionResult ListSanPham(int? page)
        {
            var ListSP = store.san_pham.OrderBy(a => a.ma);

            int pageSize = 4;
            int pageNumber = (page ?? 1);
            return View(ListSP.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult ThemSanPham()
        {
            var ListLoaiSP = store.loai_san_pham.ToList();
            ViewBag.LoaiSP = ListLoaiSP;
            return View();
        }

        [HttpPost]
        public ActionResult ThemSanPham(SanPham Model, HttpPostedFileBase file2)
        {
            var ListLoaiSP = store.loai_san_pham.ToList();
            ViewBag.LoaiSP = ListLoaiSP;

            san_pham SP = new san_pham();
            SP.maLoaiSP = Model.ma;
            SP.ten = Model.ten;
            SP.donGia = Model.dongia;
            SP.moTaTomTat = Model.motatomtat;
            SP.moTaChiTiet = Model.motachitiet;
            if (Model.trangthai == true)
            {
                SP.trangThai = 1;
            }
            else
            {
                SP.trangThai = 5;
            }

            if (file2 != null)
            {
                var randomText = new StringBuilder();
                const string alphabets = "abcdefghijklmnopqrstuvwxyz";
                var r = new Random();
                for (var j = 0; j <= 3; j++)
                {
                    randomText.Append(alphabets[r.Next(alphabets.Length)]);
                }
                var fileName2 = Path.GetFileName(file2.FileName);
                string[] Temp2 = fileName2.Split('.');
                string TenHinh2 = "";
                byte[] Mang2 = System.Text.Encoding.UTF8.GetBytes(Temp2[0] + randomText);
                MD5CryptoServiceProvider MHMK = new MD5CryptoServiceProvider();
                Mang2 = MHMK.ComputeHash(Mang2);
                foreach (byte a in Mang2)
                {
                    TenHinh2 += a.ToString("X2");
                }
                var path2 = Path.Combine(Server.MapPath("~/Content/Images/sanpham"), TenHinh2 + "." + Temp2[1]);
                file2.SaveAs(path2);

                SP.hinh = TenHinh2 + "." + Temp2[1];
            }
            store.san_pham.AddObject(SP);
            store.SaveChanges();

            return View();
        }

        public ActionResult SuaSanPham(int id)
        {
            var SP = store.san_pham.Where(a => a.ma == id).FirstOrDefault();

            SanPham VSP = new SanPham();
            VSP.ma = SP.maLoaiSP.Value;
            VSP.masp = SP.ma;
            VSP.ten = SP.ten;
            VSP.dongia = SP.donGia.Value;
            VSP.motachitiet = SP.moTaChiTiet;
            VSP.motatomtat = SP.moTaTomTat;
            if (SP.trangThai == 1)
            {
                VSP.trangthai = true;
            }
            else
            {
                VSP.trangthai = false;
            }

            var ListLoaiSP = store.loai_san_pham.ToList();
            ViewBag.LoaiSP = ListLoaiSP;
            return View(VSP);
        }

        [HttpPost]
        public ActionResult SuaSanPham(SanPham Model, HttpPostedFileBase file2)
        {
            var SP = store.san_pham.Where(a => a.ma == Model.masp).FirstOrDefault();
            SP.maLoaiSP = Model.ma;
            SP.ten = Model.ten;
            SP.donGia = Model.dongia;
            SP.moTaTomTat = Model.motatomtat;
            SP.moTaChiTiet = Model.motachitiet;
            if (Model.trangthai == true)
            {
                SP.trangThai = 1;
            }
            else
            {
                SP.trangThai = 5;
            }

            if (file2 != null)
            {
                var randomText = new StringBuilder();
                const string alphabets = "abcdefghijklmnopqrstuvwxyz";
                var r = new Random();
                for (var j = 0; j <= 3; j++)
                {
                    randomText.Append(alphabets[r.Next(alphabets.Length)]);
                }
                var fileName2 = Path.GetFileName(file2.FileName);
                string[] Temp2 = fileName2.Split('.');
                string TenHinh2 = "";
                byte[] Mang2 = System.Text.Encoding.UTF8.GetBytes(Temp2[0] + randomText);
                MD5CryptoServiceProvider MHMK = new MD5CryptoServiceProvider();
                Mang2 = MHMK.ComputeHash(Mang2);
                foreach (byte a in Mang2)
                {
                    TenHinh2 += a.ToString("X2");
                }
                var path2 = Path.Combine(Server.MapPath("~/Content/Images/sanpham"), TenHinh2 + "." + Temp2[1]);
                file2.SaveAs(path2);

                SP.hinh = TenHinh2 + "." + Temp2[1];
            }

            store.SaveChanges();

            return View(Model);
        }
        public ActionResult XoaSanPham( int id)
        {
            var SP = store.san_pham.Where(a => a.ma == id).FirstOrDefault();
            SP.trangThai = 5;
            store.SaveChanges();
            return RedirectToAction("ListSanPham");
        }
    }
}
